package com.smarthealthring.mm.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;

public class MyRefreshLayout extends SmartRefreshLayout {

    public MyRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    int downY = 0;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        int moveY = 0;
        ScrollView scrollView = (ScrollView) getParent().getParent();
        int scrollY = scrollView.getScrollY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = (int) ev.getY();
                // 保证子View能够接收到Action_move事件
                if(scrollY == 0)
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                moveY = (int) ev.getY();
                if(scrollY == 0){
                    if (moveY - downY > 20) {
                        getParent().requestDisallowInterceptTouchEvent(true);
                    }
                }

                if(moveY < downY){
                    getParent().requestDisallowInterceptTouchEvent(false);
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            case MotionEvent.ACTION_UP:
                break;

        }
        return super.dispatchTouchEvent(ev);
    }
}
